home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
cppdll
/
console.cxx
< prev
next >
Wrap
Text File
|
1990-03-07
|
11KB
|
384 lines
// written by A.P.Mazur
#include "console.hxx"
#undef NULL
#include "fontsel.h"
#include "colorsel.h"
#include "stdlib.h"
#include "stdio.h"
#include "defines.h"
#include <string.h>
#define MEM_FLAG (LMEM_FIXED)
#define COLOR_NOTIFY 302
#define FONT_NOTIFY 303
typedef int (FAR PASCAL *CALLBACK)(...);
extern "C" {
export long FAR PASCAL ConsoleWindowProc(HWND hWnd,WORD message,WORD wParam,LONG lParam );
local void ConsolePaint(HWND hWnd,LPPAINTSTRUCT lpPs);
local void updateScrollBars(HWND hWnd);
}
local HANDLE hInst = NULL;
local const char className[] = "A C++ Console";
local char buffer[30]; // temporary buffer used for conversions
BOOL Console::registered = FALSE;
Console::Console(LPSTR caption)
{
WNDCLASS TemplateClass;
background = RGB(0xff,0xff,0xff);
foreground = RGB(0,0,0);
firstLine = 0;
count = -1;
textHeight = 14;
font.lfHeight = 14;
font.lfWidth = 8;
font.lfEscapement = 0;
font.lfOrientation = 0;
font.lfWeight = 400;
font.lfItalic = 0;
font.lfUnderline = 0;
font.lfStrikeOut = 0;
font.lfCharSet = 0;
font.lfOutPrecision = OUT_DEFAULT_PRECIS;
font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
font.lfQuality = DEFAULT_QUALITY;
font.lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
strcpy((char *)font.lfFaceName,"System");
if (!registered) {
TemplateClass.hCursor = LoadCursor(NULL, IDC_ARROW);
TemplateClass.hIcon = LoadIcon(hInst,"icon");
TemplateClass.cbClsExtra = 0;
TemplateClass.cbWndExtra = 4;
TemplateClass.lpszMenuName = "menu";
TemplateClass.lpszClassName = (LPSTR)className;
TemplateClass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
TemplateClass.hInstance = hInst;
TemplateClass.lpfnWndProc = ConsoleWindowProc;
TemplateClass.style = CS_VREDRAW | CS_HREDRAW;
if (!RegisterClass((LPWNDCLASS)&TemplateClass))
return ;
registered = TRUE;
}
handle = CreateWindow((LPSTR)className,
(LPSTR)caption,
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
(HWND)NULL,
(HMENU)NULL,
(HANDLE)hInst,
(LPSTR)NULL
);
SetWindowLong(handle,0,(long)this);
newLine();
}
Console::~Console()
{
if (IsWindow(handle)) {
DestroyWindow(handle);
handle = NULL;
}
}
int Console::runModeless()
{
MSG msg;
while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
TranslateMessage((LPMSG)&msg);
DispatchMessage((LPMSG)&msg);
}
return (int)msg.wParam;
}
void Console::show(short mode)
{
ShowWindow( handle, mode );
}
void Console::update()
{
UpdateWindow( handle );
}
void Console::newLine()
{
if (count >= MAX_LINES - 1) {
if (text[0])
LocalFree(text[0]);
for(int i=0; i < count; i++) {
text [i] = text [i+1];
length[i] = length[i+1];
}
}
else
count++;
text[count] = LocalAlloc(MEM_FLAG | LMEM_ZEROINIT,2);
length[count] = 0;
updateScrollBars(handle);
UpdateWindow(handle);
}
Console& Console::operator << (LPSTR s)
{
char *line;
char *target;
HANDLE h,t;
if (s != NULL) {
t = h = LocalAlloc(MEM_FLAG | LMEM_ZEROINIT,strlen(s)+length[count]+2);
if (h) {
line = LocalLock(text[count]);
target = LocalLock(h);
strcpy(target,line);
strcpy(target+length[count],s);
LocalUnlock(text[count]);
LocalUnlock(h);
length[count] += strlen(s);
LocalFree(text[count]);
text[count] = t;
}
}
return *this;
}
Console& Console::operator <<(char c)
{
buffer[0] = c;
buffer[1] = '\0';
return (*this << buffer);
}
Console& Console::operator <<(int i)
{
itoa(i,buffer,10);
return (*this << buffer);
}
Console& Console::operator <<(long l)
{
sprintf(buffer,"%ld",l);
return (*this << buffer);
}
Console& Console::operator <<(DWORD l)
{
sprintf(buffer,"%lu",l);
return (*this << buffer);
}
Console& Console::operator <<(WORD l)
{
sprintf(buffer,"%u",l);
return (*this << buffer);
}
Console& Console::operator << (double d)
{
sprintf(buffer,"%f",d);
return (*this << buffer);
}
extern "C" {
local void updateScrollBars(HWND hWnd)
{
Console far *self;
self = (Console far *)GetWindowLong(hWnd,0);
SetScrollRange(hWnd,SB_VERT,0,self->count-1,FALSE);
SetScrollPos(hWnd,SB_VERT,self->firstLine,TRUE);
}
local void scrollDown(HWND hWnd,int lines)
{
Console far *self;
self = (Console far *)GetWindowLong(hWnd,0);
if (self->firstLine == self->count - 1)
return;
self->firstLine += lines;
if (self->firstLine > self->count - 1)
self->firstLine = self->count - 1;
updateScrollBars(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
UpdateWindow(hWnd);
}
local void scrollUp(HWND hWnd,int lines)
{
Console far *self;
self = (Console far *)GetWindowLong(hWnd,0);
if (self->firstLine == 0)
return;
self->firstLine -= lines;
if (self->firstLine < 0)
self->firstLine = 0;
updateScrollBars(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
UpdateWindow(hWnd);
}
export long FAR PASCAL ConsoleWindowProc(HWND hWnd,WORD message,WORD wParam,LONG lParam )
{
PAINTSTRUCT ps;
Console far *self;
self = (Console far *)GetWindowLong(hWnd,0);
switch (message) {
case WM_DESTROY:
PostQuitMessage( 0 );
break;
case WM_PAINT:
BeginPaint(hWnd,&ps);
ConsolePaint(hWnd,&ps);
EndPaint(hWnd,&ps);
break;
case WM_VSCROLL:
switch (wParam) {
case SB_THUMBPOSITION:
self->firstLine = LOWORD(lParam);
scrollUp(hWnd,0);
break;
case SB_BOTTOM:
self->firstLine = self->count - 1;
scrollUp(hWnd,0);
break;
case SB_TOP:
self->firstLine = 0;
scrollUp(hWnd,0);
break;
case SB_PAGEDOWN:
scrollDown(hWnd,16);
break;
case SB_PAGEUP:
scrollUp(hWnd,16);
break;
case SB_LINEDOWN:
scrollDown(hWnd,1);
break;
case SB_LINEUP:
scrollUp(hWnd,1);
break;
};
break;
case WM_COMMAND:
switch (wParam) {
case CHANGE_FONT:
{
HANDLE hLibrary = LoadLibrary("FONTSEL.DLL");
if (hLibrary > 32) {
CALLBACK lpSelectFont = (CALLBACK)GetProcAddress(hLibrary,"SelectFont");
if (lpSelectFont != NULL){
HDC hDC = GetDC(hWnd);
(*lpSelectFont)(hWnd,hDC,FIXED_PITCH | VARIABLE_PITCH,(LPLOGFONT)&(self->font),FONT_NOTIFY);
ReleaseDC(hWnd,hDC);
}
FreeLibrary(hLibrary);
}
}
break;
case CHANGE_FOREGROUND:
{
HANDLE hLibrary = LoadLibrary("COLORSEL.DLL");
if (hLibrary > 32) {
CALLBACK lpSelectColor = (CALLBACK)GetProcAddress(hLibrary,"SelectColor");
if (lpSelectColor != NULL)
(*lpSelectColor)(hWnd,(LONG FAR *) &(self->foreground),COLOR_NOTIFY);
FreeLibrary(hLibrary);
}
}
break;
case CHANGE_BACKGROUND:
{
HANDLE hLibrary = LoadLibrary("COLORSEL.DLL");
if (hLibrary > 32) {
CALLBACK lpSelectColor = (CALLBACK)GetProcAddress(hLibrary,"SelectColor");
if (lpSelectColor != NULL)
(*lpSelectColor)(hWnd,(LONG FAR *) &(self->background),COLOR_NOTIFY);
FreeLibrary(hLibrary);
}
}
break;
case COLOR_NOTIFY:
case FONT_NOTIFY:
UpdateWindow(hWnd);
break;
}
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam );
break;
}
return 0L;
}
local void ConsolePaint(HWND hWnd,LPPAINTSTRUCT lpPs)
{
TEXTMETRIC tm;
char *line;
Console far *self;
HANDLE hLogFont;
HANDLE hLogFontOld;
int yPos;
self = (Console far *)GetWindowLong(hWnd,0);
hLogFont = (HANDLE)CreateFontIndirect((LPLOGFONT)&(self->font));
hLogFontOld = SelectObject(lpPs->hdc,hLogFont);
/* get font parameters */
SetBkColor (lpPs->hdc,self->background);
SetTextColor(lpPs->hdc,self->foreground);
GetTextMetrics(lpPs->hdc,(LPTEXTMETRIC)&tm );
self->textHeight = tm.tmHeight + tm.tmExternalLeading;
yPos = 3;
for (int i=(self->firstLine); (i <= (self->count)) && (yPos < lpPs->rcPaint.bottom); i++) {
line = LocalLock((self->text)[i]);
if (line) {
if (((yPos + self->textHeight) > lpPs->rcPaint.top) &&
((yPos) < lpPs->rcPaint.bottom))
TextOut( lpPs->hdc, 3,yPos,line,strlen(line));
yPos += self->textHeight;
LocalUnlock((self->text)[i]);
}
}
SelectObject( lpPs->hdc, hLogFontOld );
DeleteObject( hLogFont );
}
export int FAR PASCAL
LibMain(HANDLE hInstance,WORD dataSeg,WORD heapSize)
{
hInst = hInstance;
if (heapSize == 0)
return 0;
return LocalInit(dataSeg,NULL,heapSize);
}
} // end of "C" linkage